const { Controller } = require('egg');

class BarcodeController extends Controller {


  /**

   * @param req
   * @param res
   * @api {GET} /productManage/barcode/list 获取条码列表
   * @apiName list
   * @apiGroup barcode
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiQuery  { String } name 名称
   * @apiQuery  { String } barcode 条码
   * @apiQuery  { String } order 排序字段
   * @apiQuery  { String } method 排序方式
   * @apiQuery  { Number } pageIndex 页码
   * @apiQuery  { Number } pageSize 页面容量
   *
   * @apiHeader { String } Authorization 注册信息
   */
  async list() {
    const ctx = this.ctx;
    const app = this.app;
    try {
      const value = ctx.query;

      const { product_id, name, barcode, method, order, pageIndex, pageSize } = await app.rule.barcode.list(value);

      const data = await ctx.service.barcode.list(product_id, name, barcode, method, order, pageIndex, pageSize);
      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 200;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }


  /**
   * @api {POST} /productManage/barcode/insert 新增
   * @apiName insert
   * @apiGroup barcode
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiBody  { String } product_id 产品id
   * @apiBody  { String } barcode 类型
   *
   * @apiHeader { String } Authorization 注册信息
   */
  async insert() {
    const ctx = this.ctx;
    const app = this.app;
    try {

      const value = ctx.request.body;

      const { product_id, barcode } = await app.rule.barcode.insert(value);

      const data = await ctx.service.barcode.insert(product_id, barcode);

      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 200;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }


  /**
   * @api {POST} /productManage/barcode/delete 删除
   * @apiName delete
   * @apiGroup barcode
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiBody  { String } product_id 产品id
   * @apiBody  { String } barcode 类型
   *
   * @apiHeader { String } Authorization 注册信息
   */
  async delete() {
    const ctx = this.ctx;
    try {
      const value = ctx.request.body;

      const { ids } = await ctx.app.rule.barcode.delete(value);

      const data = await ctx.service.barcode.delete(ids);

      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 200;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }

}

module.exports = BarcodeController;
